package com.hadwinling.blog.controller;

import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.service.AdminService;
import com.hadwinling.blog.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/23 8:22
 */
@Controller
public class UserAccountController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private FileUpload fileUpload;

    @GetMapping("/user/account")
    public String toAccountPage(HttpServletRequest request, Model model){
        Admin user = (Admin) request.getSession().getAttribute("currUser");
        model.addAttribute("user",user);
        return "UserCenter/account";
    }

    /**
     * 图片上传
     * @param photo
     * @param request
     * @return
     */
    @PostMapping("/user/userPhoto")
    @ResponseBody
    public Map<String, String> uploadPhoto(MultipartFile photo, HttpServletRequest request) throws IOException {
        System.out.println("上传头像");
        Map<String, String> ret = new HashMap<String, String>();
        String filepath = fileUpload.FileUploadToQiNiu(photo);
        Admin user = (Admin) request.getSession().getAttribute("currUser");
        int result = adminService.updatePhotoById(filepath,user.getId());
        ret.put("type", "success");
        ret.put("msg", "上传头像成功！");
        ret.put("filepath", filepath);
        return ret;
    }

    /**
     * 跳转到修改个人信息页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/user/updateUser")
    public String toUpdateUserPage(HttpServletRequest request, Model model){
        Admin user = (Admin) request.getSession().getAttribute("currUser");
        model.addAttribute("user",user);
        return "UserCenter/accountUpdate";
    }

    /**
     * 执行修改个人信息操作
     * @param user
     * @param model
     * @param request
     * @return
     */
    @PostMapping("/user/updateUser")
    public String updateUser(Admin user,Model model,HttpServletRequest request){
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        user.setId(currUser.getId());
        adminService.updateByPrimaryKeySelective(user);
        return "redirect:/user/account";
    }

    /**
     * 跳转到修改密码页面
     * @return
     */
    @GetMapping("/user/password")
    public String toPasswordPage(){
        return "UserCenter/password";
    }

    /**
     * 检查密码是否正确
     * @param password
     * @param request
     * @return
     */
    @PostMapping("/user/checkpassword")
    @ResponseBody
    public String updatePassword(String password,HttpServletRequest request){
//        System.out.println("输入的："+password);
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
//        System.out.println("currUserPassword:"+currUser.getPassword());
        if (password.equals(currUser.getPassword())){
            //密码正确
            return "1";
        }else{
            //密码错误
            return "0";
        }
    }

    /**
     * 执行变更密码操作
     * @param password
     * @param request
     * @return
     */
    @PostMapping("/user/password")
    public String password(String password,HttpServletRequest request){
        System.out.println("输入的："+password);
        Admin currUser = (Admin) request.getSession().getAttribute("currUser");
        adminService.updatePasswordById(password,currUser.getId());
        return "redirect:/user/account";
    }


}
